﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ViewStationeryCatalogue : System.Web.UI.Page
    {
        ViewStationeryCatalogueControl viewcont;
        StationeryInventoryEntities1 ent;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                viewcont = new ViewStationeryCatalogueControl();
                ent = new StationeryInventoryEntities1();

                if (!IsPostBack)
                {
                    ddlCategory.DataSource = viewcont.getCatList();
                    ddlCategory.DataTextField = "CategoryName";
                    ddlCategory.DataValueField = "CategoryID";
                    ddlCategory.DataBind();
                    ddlCategory.Items.Insert(0, "All");
                }

                Binding();
            }
            catch (Exception ex)
            {
            }

            
        }

        public void Binding()
        {
            dgvCatalogue.DataSource = viewcont.getAllStationeryList();
            dgvCatalogue.DataBind();
        }

        protected void ddlCategory_SelectedIndexChanged1(object sender, EventArgs e)
        {
            try
            {
                if (ddlCategory.SelectedValue == "All")
                {
                    Binding();
                }
                else
                {
                    int catId = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
                    dgvCatalogue.DataSource = viewcont.getAllStationeryListByCatID(catId);
                    dgvCatalogue.DataBind();

                }
            }
            catch (Exception ex)
            {
            }

           
         }

        protected void dgvCatalogue_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                dgvCatalogue.PageIndex = e.NewPageIndex;
                //Binding();
                GenerateGridView();
            }
            catch (Exception ex)
            {
            }

           
        }

        private void GenerateGridView(){
            if (ddlCategory.SelectedValue == "All")
            {
                Binding();
            }
            else
            {
                int catId = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
                dgvCatalogue.DataSource = viewcont.getAllStationeryListByCatID(catId);
                dgvCatalogue.DataBind();

            }

        }


    }
}